﻿Imports System.Runtime.CompilerServices

Namespace Pop.Api.Util

    Friend Module RequestValidator

        Private Const ERR_CODE_PARAM_MISSING As String = "40"
        Private Const ERR_CODE_PARAM_INVALID As String = "41"
        Private Const ERR_MSG_PARAM_MISSING As String = "client-error:Missing required arguments:{0}"
        Private Const ERR_MSG_PARAM_INVALID As String = "client-error:Invalid arguments:{0}"

        ''' <summary>
        ''' 验证字符串是否为空。是: True 非空: False
        ''' </summary>
        ''' <param name="value">参数值</param>
        <Extension()>
        Public Function IsRequired(ByVal value As String) As Boolean
            Return value Is Nothing OrElse String.IsNullOrEmpty(value)
        End Function

        ''' <summary>
        ''' 验证参数是否为空。
        ''' </summary>
        ''' <param name="value">参数值</param>
        ''' <param name="name">参数名</param>
        <Extension()>
        Public Sub ValidateRequired(ByVal value As Object, ByVal name As String)
            If value Is Nothing Then
                Throw New PopException(ERR_CODE_PARAM_MISSING, String.Format(ERR_MSG_PARAM_MISSING, name))
            Else
                If value.[GetType]() Is GetType(String) Then
                    Dim strValue As String = TryCast(value, String)
                    If String.IsNullOrEmpty(strValue) Then
                        Throw New PopException(ERR_CODE_PARAM_MISSING, String.Format(ERR_MSG_PARAM_MISSING, name))
                    End If
                End If
            End If
        End Sub

        ''' <summary>
        ''' 验证字符串参数的最大长度。
        ''' </summary>
        ''' <param name="name">参数名</param>
        ''' <param name="value">参数值</param>
        ''' <param name="maxLength">最大长度</param>
        <Extension()>
        Public Sub ValidateMaxLength(ByVal value As String, ByVal name As String, ByVal maxLength As Integer)
            If value IsNot Nothing AndAlso value.Length > maxLength Then
                Throw New PopException(ERR_CODE_PARAM_INVALID, String.Format(ERR_MSG_PARAM_INVALID, name))
            End If
        End Sub

        ''' <summary>
        ''' 验证上传文件的最大字节长度。
        ''' </summary>
        ''' <param name="name">参数名</param>
        ''' <param name="value">参数值</param>
        ''' <param name="maxLength">最大长度</param>
        <Extension()>
        Public Sub ValidateMaxLength(ByVal value As FileItem, ByVal name As String, ByVal maxLength As Integer)
            If value IsNot Nothing AndAlso value.GetContent() IsNot Nothing AndAlso value.GetContent().Length > maxLength Then
                Throw New PopException(ERR_CODE_PARAM_INVALID, String.Format(ERR_MSG_PARAM_INVALID, name))
            End If
        End Sub

        ''' <summary>
        ''' 验证以逗号分隔的字符串参数的最大列表长度。
        ''' </summary>
        ''' <param name="name">参数名</param>
        ''' <param name="value">参数值</param>
        ''' <param name="maxSize">最大列表长度</param>
        <Extension()>
        Public Sub ValidateMaxListSize(ByVal value As String, ByVal name As String, ByVal maxSize As Integer)
            If value IsNot Nothing Then
                Dim data As String() = value.Split(","c)
                If data IsNot Nothing AndAlso data.Length > maxSize Then
                    Throw New PopException(ERR_CODE_PARAM_INVALID, String.Format(ERR_MSG_PARAM_INVALID, name))
                End If
            End If
        End Sub

        ''' <summary>
        ''' 验证字符串参数的最小长度。
        ''' </summary>
        ''' <param name="name">参数名</param>
        ''' <param name="value">参数值</param>
        ''' <param name="minLength">最小长度</param>
        <Extension()>
        Public Sub ValidateMinLength(ByVal value As String, ByVal name As String, ByVal minLength As Integer)
            If value IsNot Nothing AndAlso value.Length < minLength Then
                Throw New PopException(ERR_CODE_PARAM_INVALID, String.Format(ERR_MSG_PARAM_INVALID, name))
            End If
        End Sub

        ''' <summary>
        ''' 验证数字参数的最大值。
        ''' </summary>
        ''' <param name="name">参数名</param>
        ''' <param name="value">参数值</param>
        ''' <param name="maxValue">最大值</param>
        <Extension()>
        Public Sub ValidateMaxValue(ByVal value As Nullable(Of Long), ByVal name As String, ByVal maxValue As Long)
            If value IsNot Nothing AndAlso value > maxValue Then
                Throw New PopException(ERR_CODE_PARAM_INVALID, String.Format(ERR_MSG_PARAM_INVALID, name))
            End If
        End Sub

        ''' <summary>
        ''' 验证数字参数的最小值。
        ''' </summary>
        ''' <param name="name">参数名</param>
        ''' <param name="value">参数值</param>
        ''' <param name="minValue">最小值</param>
        <Extension()>
        Public Sub ValidateMinValue(ByVal value As Nullable(Of Long), ByVal name As String, ByVal minValue As Long)
            If value IsNot Nothing AndAlso value < minValue Then
                Throw New PopException(ERR_CODE_PARAM_INVALID, String.Format(ERR_MSG_PARAM_INVALID, name))
            End If
        End Sub

    End Module

End Namespace